package com.puff.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.puff.constant.MessageConstant;
import com.puff.dto.ShopCategoryDTO;
import com.puff.dto.ShopCategoryPageQueryDTO;
import com.puff.entity.Orders;
import com.puff.entity.Shop;
import com.puff.entity.ShopCategory;
import com.puff.exception.DeletionNotAllowedException;
import com.puff.mapper.OrderMapper;
import com.puff.mapper.ShopCategoryMapper;
import com.puff.mapper.ShopMapper;
import com.puff.result.PageResult;
import com.puff.service.OrderService;
import com.puff.service.ShopCategoryService;
import com.puff.vo.DishVO;
import com.puff.vo.ShopCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ShopCategoryServiceImpl implements ShopCategoryService {

    @Autowired
    private ShopCategoryMapper shopCategoryMapper;

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 新增商家分类
     * @param shopCategoryDTO
     */
    public void saveShopCategory(ShopCategoryDTO shopCategoryDTO) {
        ShopCategory shopCategory = new ShopCategory();
        BeanUtils.copyProperties(shopCategoryDTO,shopCategory);

        shopCategoryMapper.insert(shopCategory);
    }

    /**
     * 商家分类分页查询
     * @param shopCategoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(ShopCategoryPageQueryDTO shopCategoryPageQueryDTO) {
        // 利用PageHelper分页查询
        PageHelper.startPage(shopCategoryPageQueryDTO.getPage(),shopCategoryPageQueryDTO.getPageSize());

        // 菜品分页查询给前端返回的数据类型
        Page<ShopCategoryVO> page = shopCategoryMapper.pageQuery(shopCategoryPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除商家分类
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        // 首先检查每个分类下是否存在商家，如果有，则不能删除
        for (Long id : ids) {
            Shop shop = shopMapper.getByShopCategoryId(id);
            if (shop != null){
                //说明该商家分类下，存在商家，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.SHOPCATEGORY_BE_RELATED_BY_SHOP);
            }
        }

        shopCategoryMapper.deleteByIds(ids);
    }

    /**
     * 查询所有商家分类
     * @return
     */
    public List<ShopCategory> list() {
        List<ShopCategory> list = shopCategoryMapper.list();
        return list;
    }

    /**
     * 根据id查询商家分类信息
     * @param id
     * @return
     */
    public ShopCategoryVO getShopCategoryById(Long id) {
        ShopCategory shopCategory = shopCategoryMapper.getById(id);

        ShopCategoryVO shopCategoryVO = new ShopCategoryVO();
        BeanUtils.copyProperties(shopCategory,shopCategoryVO);
        return shopCategoryVO;
    }

    /**
     * 修改商家分类信息
     * @param shopCategoryDTO
     */
    public void updateShopCategory(ShopCategoryDTO shopCategoryDTO) {
        ShopCategory shopCategory = new ShopCategory();
        BeanUtils.copyProperties(shopCategoryDTO,shopCategory);

        shopCategoryMapper.update(shopCategory);
    }
}
